import os
import json
from pathlib import Path

file_list = []


def projectParse(path, indent=""):
    result = {}
    for entry in Path(path).iterdir():
        if entry.name.startswith('.'):
            continue
        if entry.is_dir():
            result[entry.name] = projectParse(entry, indent + "  ")
        else:
            try:
                with open(entry, 'r', encoding='utf-8') as file:
                    content = file.read()
                result[entry.name] = content
            except Exception as e:
                result[entry.name] = f"Error reading file: {e}"
    return result


def setupJson(title, core_path):
    directory_path = f"thesisDirectory/{title}"
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)
    save_path = directory_path + f'/{title}_parse.json'
    print(save_path)
    json_data = projectParse(core_path)
    # 将结果保存到 JSON 文件
    with open(save_path, 'w', encoding='utf-8') as f:
        json.dump(json_data, f, indent=4)  # 使用 json.dump 直接将数据写入文件


setupJson("基于YOLOv5的羽毛球落点判断算法的设计与实现", "D:/pycode/yolov5")
